using System;
using System.Web.UI;
using System.ComponentModel;
using System.Collections;
using System.Text;

namespace com.flajaxian.AWS
{
    /// <summary>
    /// Collection of AccessControl objects
    /// </summary>
    [ParseChildren(true), PersistChildren(false), TypeConverter(typeof(ExpandableObjectConverter))]
    public class AccessControlCollection : CollectionBase
    {
        /// <summary>
        /// 
        /// </summary>
        public AccessControlCollection()
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ac"></param>
        /// <returns></returns>
        public int Add(AccessControl ac)
        {
            return base.List.Add(ac);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ac"></param>
        /// <returns></returns>
        public bool Contains(AccessControl ac)
        {
            return base.List.Contains(ac);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ac"></param>
        /// <returns></returns>
        public int IndexOf(AccessControl ac)
        {
            return base.List.IndexOf(ac);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <param name="ac"></param>
        public void Insert(int index, AccessControl ac)
        {
            base.List.Insert(index, ac);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ac"></param>
        public void Remove(AccessControl ac)
        {
            base.List.Remove(ac);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (this.Count == 0)
            {
                return String.Empty;
            }
            return String.Format(SR.AccessControlListXml, this.GetGrantsXml());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public AccessControl this[int index]
        {
            get
            {
                return (base.List[index] as AccessControl);
            }
            set
            {
                base.List[index] = value;
            }
        }


        private string GetGrantsXml()
        {
            StringBuilder sb = new StringBuilder();
            foreach (AccessControl ac in this)
            {
                sb.Append(ac.ToGrant().ToXml());
            }
            return sb.ToString();
        }

    }
}
